package top.malaoshi.data;

import java.util.ArrayList;
import java.util.List;

public class DataTables {


    private List<DataTable> tableList;

    public DataTables() {
        tableList = new ArrayList<>();
    }

    public void add(DataTable table){
        tableList.add(table);
    }

    /**
     * 根据索引获取 DataTable
     * @param index
     * @return
     */
    public DataTable get(int index){
        return tableList.get(index);
    }

    /**
     * 根据表名获取 DataTable
     * @param tableName
     * @return
     */
    public DataTable get(String tableName){

        for(DataTable table:this.tableList){
            if(table.getName().equals(tableName)){
                return table;
            }
        }
        return null;
    }

    /**
     * 根据表名获取该表所在的索引
     * @param tableName
     * @return
     */
    public int getIndex(String tableName){

        for(int i=0,count = this.tableList.size();i<count;i++){
            DataTable table = this.tableList.get(i);
            if(table.getName().equals(tableName)){
                return i;
            }
        }
        return -1;
    }

    /**
     * 根据索引删除表
     * @param index
     */
    public DataTable remove(int index){
        return tableList.remove(index);
    }

    /**
     * 根据表名删除表
     * @param tableName
     */
    public boolean remove(String tableName){
        int index = getIndex(tableName);
        if(index >= 0) {
            tableList.remove(index);
            return true;
        }
        return false;
    }

    /**
     * 获取 DataTable 总数
     * @return
     */
    public int count(){
        return tableList.size();
    }

    /**
     * 清空所有表
     */
    public void clear(){
        tableList.clear();
    }
}
